/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Svetlana Shikhutskaya
 */
@Entity
@Table(name = "USERS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByFirstName", query = "SELECT u FROM Users u WHERE u.firstName = :firstName"),
    @NamedQuery(name = "Users.findByIdUser", query = "SELECT u FROM Users u WHERE u.idUser = :idUser"),
    @NamedQuery(name = "Users.findByLogin", query = "SELECT u FROM Users u WHERE u.login = :login"),
    
    @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
    @NamedQuery(name = "Users.findByLastName", query = "SELECT u FROM Users u WHERE u.lastName = :lastName"),
    @NamedQuery(name = "Users.findByEMail", query = "SELECT u FROM Users u WHERE u.eMail = :eMail"),
    @NamedQuery(name = "Users.findByAddress", query = "SELECT u FROM Users u WHERE u.address = :address"),
   
    @NamedQuery(name = "Users.findByKeyword", query = "SELECT u FROM Users u WHERE upper(u.lastName) LIKE upper(:keyword) OR upper(u.firstName) LIKE upper(:keyword)"),
   
    @NamedQuery(name = "Users.SortByLastName", query = "SELECT o FROM Users o ORDER BY o.lastName"),
    @NamedQuery(name = "Users.SortByLogin", query = "SELECT o FROM Users o  ORDER BY o.login"),
    @NamedQuery(name = "Users.SortByEmail", query = "SELECT o FROM Users o ORDER BY o.eMail"),
    @NamedQuery(name = "Users.countRows", query = "SELECT COUNT(u.idUser) FROM Users u")
    


})
public class Users implements Serializable {

    private static final long serialVersionUID = 1L;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "FIRST_NAME")
    private String firstName;
    @Id
    @Basic(optional = false)
    @GeneratedValue(generator = "seq_users_id_user")
    @SequenceGenerator(name = "seq_users_id_user", sequenceName = "seq_users_id_user", allocationSize = 1)
    @NotNull
    @Column(name = "ID_USER")
    private Integer idUser;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "LOGIN")
    private String login;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "PASSWORD")
    private String password;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "LAST_NAME")
    private String lastName;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Basic(optional = false)
    @Size(min = 1, max = 100)
    @Column(name = "E_MAIL")
    private String eMail;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 300)
    @Column(name = "ADDRESS")
    private String address;
    @ManyToMany(mappedBy = "usersCollection", cascade = CascadeType.REFRESH)
    private Collection<Groups> groupsCollection;
    @ManyToMany(mappedBy = "usersCollection", cascade = CascadeType.REFRESH)
    private Collection<Role> roleCollection;
    @OneToMany(mappedBy = "idUser",cascade={CascadeType.REFRESH,CascadeType.MERGE})
    private Collection<Orders> ordersCollection;
    @OneToMany(mappedBy = "idUser",cascade=CascadeType.ALL)
    private Collection<Comments> commentsCollection;
    
    private static String findByRoleQuery= "select u.* "
            + " from users u "
            + " join user_role ur on u.id_user=ur.id_user "
            + " and id_role= ? "
            + " union "
            + " select u.* "
            + " from users u "
            + " join user_in_group ug on u.id_user=ug.id_user "
            + " join role_in_group rg on ug.id_group=rg.id_group "
            + " and rg.id_role= ? ";
    
    public static String getFindByRoleQuery(){
        return findByRoleQuery;
    
    }
            

    public Users() {
    }

    public Users(Integer idUser) {
        this.idUser = idUser;
    }

    public Users( Integer idUser, String firstName, String login, String password, String lastName, String eMail, String address) {
  
        this.idUser = idUser;
        this.firstName = firstName;
        this.login = login;
        this.password = password;
        this.lastName = lastName;
        this.eMail = eMail;
        this.address = address;
    }
        public Users(String login,Integer idUser, String firstName, String lastName, String eMail, String address) {
        this.login = login;
        this.idUser = idUser;
        this.firstName = firstName;
        this.lastName = lastName;
        this.eMail = eMail;
        this.address = address;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public Integer getIdUser() {
        return idUser;
    }

    public void setIdUser(Integer idUser) {
        this.idUser = idUser;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setRole(Role role) {
        this.roleCollection.add(role);
    }

    public void setGroup(Groups group) {
        this.groupsCollection.add(group);
    }
      public void removeGroup(Groups group) {
        this.groupsCollection.remove(group);
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEMail() {
        return eMail;
    }

    public void setEMail(String eMail) {
        this.eMail = eMail;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @XmlTransient
    public Collection<Groups> getGroupsCollection() {
        return groupsCollection;
    }

    public void setGroupsCollection(Collection<Groups> groupsCollection) {
        this.groupsCollection = groupsCollection;
    }


    @XmlTransient
    public Collection<Role> getRoleCollection() {
        for (Groups gr : this.getGroupsCollection()) {
               this.roleCollection.addAll(gr.getRoleCollection());
               Set<Role> roleSet = new HashSet<Role>(roleCollection);
               this.setRoleCollection(roleSet);
        
        }
       return roleCollection;}

    public void setRoleCollection(Collection<Role> roleCollection) {
        
        this.roleCollection = roleCollection;
        
        
        
    }

    @XmlTransient
    public Collection<Orders> getOrdersCollection() {
        return ordersCollection;
    }

    public void setOrdersCollection(Collection<Orders> ordersCollection) {
        this.ordersCollection = ordersCollection;
    }

    @XmlTransient
    public Collection<Comments> getCommentsCollection() {
        return commentsCollection;
    }

    public void setCommentsCollection(Collection<Comments> commentsCollection) {
        this.commentsCollection = commentsCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idUser != null ? idUser.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.idUser == null && other.idUser != null) || (this.idUser != null && !this.idUser.equals(other.idUser))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Users[ idUser=" + idUser + " ]";
    }
}
